package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.commons.vo.PageData;
import cn.tedu.csmall.commons.web.JsonResult;
import cn.tedu.csmall.product.pojo.param.PictureAddNewParam;
import cn.tedu.csmall.product.pojo.vo.PictureListItemVO;
import cn.tedu.csmall.product.pojo.vo.PictureStandardVO;
import cn.tedu.csmall.product.service.impl.PictureServiceImpl;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/pictures")
@Api(tags = "05. 图片管理模块")
@Slf4j
@Validated
public class PictureController {

    @Autowired
    private PictureServiceImpl pictureService;
    
    @PostMapping("/add-new")
    @ApiOperation("添加图片")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Valid PictureAddNewParam pictureAddNewParam){
        pictureService.addNew(pictureAddNewParam);
        
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据ID删除图片")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "Long")
    })
    public JsonResult delete(
            @Range(min = 1, message = "根据ID删除图片失败，请提交合法的ID值！")
            @PathVariable Long id){
        pictureService.deleteById(id);

        return JsonResult.ok();
    }

    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据ID查询图片详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "图片ID", required = true, dataType = "Long")
    })
    public JsonResult getStandardById(
            @Range(min = 1, message = "根据ID查询图片失败，请提交合法的ID值！")
            @PathVariable Long id){
        PictureStandardVO standardById = pictureService.getStandardById(id);
        return JsonResult.ok(standardById);
    }

    @GetMapping("/list-by-album")
    @ApiOperation("查询图片列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "albumId", value = "相册ID", required = true, paramType = "query", dataType = "long"),
            @ApiImplicitParam(name = "pageNum", value = "页码值", paramType = "query"),
            @ApiImplicitParam(name = "queryType", value = "查询类型", example = "all")
    })
    public JsonResult list(
            @Range(min = 1, message = "请提交有效的相册ID值！") Long albumId,
            @Range(min = 1, message = "查询图片列表失败，请提供正确的页码值!") Integer pageNum,
            String queryType){
        if(pageNum == null || pageNum < 1){
            pageNum = 1;
        }
        PageData<PictureListItemVO> pageData;
        if("all".equals(queryType)){
            pageData = pictureService.listByAlbumId(albumId, pageNum, Integer.MAX_VALUE);
        }else{
            pageData = pictureService.listByAlbumId(albumId, pageNum);
        }
        return JsonResult.ok(pageData);
    }
}
